import {isPhoneNum} from "@/utils/regular";
import {checkUsername,checkEmail,} from "@/api/user";

export const loginRules =  {
    email:[
        {
            required: true,
            message: '请输入邮箱地址',
            trigger: 'blur',
        },
        {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change'],
        },
    ],
    // user_account: [
    //     {
    //         trigger: 'blur',
    //         required: true,
    //         message: '请输入用户名/手机号',
    //     },
    //     {
    //         min: 3,
    //         max: 20,
    //         message: '用户名/手机号长度错误',
    //         trigger: 'blur'
    //     }
    // ],
    password: [
        {
            trigger: 'blur',
            required: true,
            message: '请输入密码',
        },
        {
            min: 3,
            max: 20,
            message: '密码长度错误',
            trigger: 'blur'
        }
    ],
    image_text: [
        {
            trigger: 'blur',
            required: true,
            message: '请输入验证码',
        },
        {
            min: 4,
            max: 4,
            message: '验证码长度错误',
            trigger: 'blur'
        }
    ],
}


export const regRules =  function () {
    let ruleForm = this.formLabelAlign
    return {
        email:[
            {
                required: true,
                message: '请输入邮箱地址',
                trigger: 'blur',
            },
            {
                type: 'email',
                message: '请输入正确的邮箱地址',
                trigger: ['blur', 'change'],
            },
            {
                trigger: 'blur',
                validator(rule, value, callback){
                    checkEmail(value)
                        .then(res=>{
                            if(Number(res.code)===0){
                                res.data.result?callback(new Error(res.msg)):callback()
                            }else{
                                callback(new Error(res.msg))
                            }
                        })
                },
            }
        ],
        username: [
            {
                trigger: 'blur',
                required: true,
                message: '请输入用户名',
            },
            {
                min: 3,
                max: 20,
                message: '用户名长度错误',
                trigger: 'blur'
            },
            {
                trigger: 'blur',
                validator(rule, value, callback){
                    checkUsername(value)
                        .then(res=>{
                            if(Number(res.code)===0){
                                res.data.result?callback(new Error(res.msg)):callback()
                            }else{
                                callback(new Error(res.msg))
                            }
                        })
                },
            }
        ],
        password: [
            {
                trigger: 'blur',
                required: true,
                message: '请输入密码',
            },
            {
                min: 3,
                max: 20,
                message: '密码长度错误',
                trigger: 'blur'
            },
        ],
        password_repeat: [
            {
                trigger: 'blur',
                required: true,
                message: '请再次输入密码',
            },
            {
                min: 3,
                max: 20,
                message: '两次输入密码不一致!',
                trigger: 'blur'
            },
            {
                trigger: 'blur',
                validator(rule, value, callback){
                    if (value !== ruleForm.password) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                },
            }
        ],
        // mobile: [
        //     {
        //         trigger: 'blur',
        //         required: true,
        //         message: '请输入手机号',
        //     },
        //     {
        //         min: 11,
        //         max: 11,
        //         message: '手机号长度错误',
        //         trigger: 'blur'
        //     },
        //     {
        //         trigger: 'blur',
        //         validator(rule, value, callback){
        //             if(!isPhoneNum(value)){
        //                 callback(new Error('手机号格式错误'));
        //             }else{
        //                 callback()
        //             }
        //         },
        //     }
        // ],
        image_text: [
            {
                trigger: 'blur',
                required: true,
                message: '请输入验证码',
            },
            {
                min: 4,
                max: 4,
                message: '验证码长度错误',
                trigger: 'blur'
            },
        ],
    }
}
